package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dto.ChannelDto;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "ChannelService", tags = "频道管理API")
@RestController
@RequestMapping("/api/v1/channel")
public class ChannelController {

    @Autowired
    private ChannelService channelService;

    @ApiOperation("根据频道名称分页查询")
    @PostMapping("/list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto channelDto){
        return this.channelService.findByNameAndPage(channelDto);
    }
    @ApiOperation("根据频道名称分页查询")
    @GetMapping("/channels")
    public ResponseResult findAll(){
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }

    @ApiOperation("频道新增")
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody AdChannel channel){
        return channelService.insert(channel);
    }
    @ApiOperation("频道修改")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel channel){
        if (channel.getId()==null){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id不能为空");
        }
        channelService.updateById(channel);
        return ResponseResult.okResult();

    }

    @ApiOperation("频道删除")
    @GetMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id) {
        return channelService.delete(id);
    }
}
